package com.umeng.soexample.mvp.view.fragment;


import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jwenfeng.library.pulltorefresh.BaseRefreshListener;
import com.jwenfeng.library.pulltorefresh.PullToRefreshLayout;
import com.umeng.lib_core.base.BaseFragment;
import com.umeng.soexample.R;
import com.umeng.soexample.bean.BrandGoodsEntity;
import com.umeng.soexample.bean.GoodsListEntity;
import com.umeng.soexample.bean.ShopGoodsEntity;
import com.umeng.soexample.di.compont.DaggerShopDetailsFragmentCompont;
import com.umeng.soexample.di.modules.ShopDetailsFragmentModules;
import com.umeng.soexample.mvp.contract.ShopDetailsFragmentContract;
import com.umeng.soexample.mvp.persenter.ShopDetailsFragmentPersenter;
import com.umeng.soexample.mvp.view.adapter.HomeMenuAdapter;
import com.umeng.soexample.mvp.view.adapter.ShopDetailsAdapter;

/**
 * 店铺详情界面
 */
public class ShopDetailsFragment extends BaseFragment<ShopDetailsFragmentPersenter>
        implements ShopDetailsFragmentContract.ShopDetailsFragmentIView, View.OnClickListener, BaseRefreshListener {

    private int page = 1;
    private View view;
    private RadioGroup rg_shop_details;//隐藏的单选按钮组
    private RadioGroup rg_shop_details_all_goods_type;//隐藏的全部商品下的单选按钮组
    private RadioButton rb_shop_details_all_goods_default;//隐藏的全部商品下的单选按钮组 -> 默认
    private boolean cx = false;
    private boolean first = true;
    private RecyclerView rv_shop_details;//列表
    private ShopDetailsAdapter adapter;
    private PullToRefreshLayout ptrl_shop_details;//刷新布局
    //TODO 头布局
    private View view_head;
    private RelativeLayout rl_item_head_shop_details_bg;//头布局背景
    private int totalY = 0;
    private int rl_item_head_shop_details_bg_Height = 0;
    private ImageView iv_item_head_shop_details_shop_tou;//店铺头像
    private TextView tv_item_head_shop_details_shop_name;//店铺名称
    private TextView tv_item_head_shop_details_shop_fs;//店铺粉丝
    private RadioGroup rg_item_head_shop_details;//店铺按钮组
    private RadioGroup rg_item_head_shop_details_all_goods_type;//全部商品下按钮组
    private RadioButton rb_item_head_shop_details_all_goods_default;//头布局全部商品下按钮组 -> 默认
    private SlidingMenu slidingMenu;
    @Override
    public View createView(LayoutInflater inflater) {
        view = inflater.inflate(R.layout.fragment_shop_details, null);
        return view;
    }

    @Override
    public void init(View view) {

        rg_shop_details = view.findViewById(R.id.rg_shop_details);//隐藏的按钮组
        rg_shop_details_all_goods_type = view.findViewById(R.id.rg_shop_details_all_goods_type);//隐藏的全部商品下的单选按钮组
        rv_shop_details = view.findViewById(R.id.rv_shop_details);//按钮组
        adapter = new ShopDetailsAdapter(R.layout.item_shop_details);
        ptrl_shop_details = view.findViewById(R.id.ptrl_shop_details);//按钮组
        //TODO 头布局
        view_head = View.inflate(getContext(), R.layout.item_head_shop_details, null);
        rl_item_head_shop_details_bg = view_head.findViewById(R.id.rl_item_head_shop_details_bg);//头布局背景
        //测量高度
        rl_item_head_shop_details_bg.measure(0, 0);
        //获取measure高度
        rl_item_head_shop_details_bg_Height = rl_item_head_shop_details_bg.getMeasuredHeight();
        iv_item_head_shop_details_shop_tou = view_head.findViewById(R.id.iv_item_head_shop_details_shop_tou);//店铺头像
        tv_item_head_shop_details_shop_name = view_head.findViewById(R.id.tv_item_head_shop_details_shop_name);//店铺名称
        tv_item_head_shop_details_shop_fs = view_head.findViewById(R.id.tv_item_head_shop_details_shop_fs);//店铺粉丝
        rg_item_head_shop_details = view_head.findViewById(R.id.rg_item_head_shop_details);//店铺按钮组
        rg_item_head_shop_details_all_goods_type = view_head.findViewById(R.id.rg_item_head_shop_details_all_goods_type);//全部商品下按钮组

        rv_shop_details.setLayoutManager(new GridLayoutManager(getContext(), 2, GridLayoutManager.VERTICAL, false));
        adapter.addHeaderView(view_head, 0);
        rv_shop_details.setAdapter(adapter);

    }

    @Override
    public void initData() {
        p.goodsListRequest("1", "" + page);
        p.brandGoodsRequest("1", "1");
//        p.shopGoodsRequest("1", "1");
    }

    @Override
    public void setupDagger() {
        DaggerShopDetailsFragmentCompont.builder().shopDetailsFragmentModules(new ShopDetailsFragmentModules(this))
                .build().inject(this);
    }

    @Override
    public void initListener() {

        view.findViewById(R.id.iv_shop_details_back).setOnClickListener(this);//返回上一页
        view.findViewById(R.id.ll_shop_details_search).setOnClickListener(this);//搜索框
        view.findViewById(R.id.iv_shop_details_category).setOnClickListener(this);//品类
        view.findViewById(R.id.iv_shop_details_share).setOnClickListener(this);//分享
        view.findViewById(R.id.tv_shop_details_spfl).setOnClickListener(this);//商品分类
        view.findViewById(R.id.tv_shop_details_dpjj).setOnClickListener(this);//店铺简介
        view.findViewById(R.id.rb_shop_details_all_goods_default).setOnClickListener(this);//隐藏的全部商品下的单选按钮组 -> 默认
        view_head.findViewById(R.id.rb_item_head_shop_details_all_goods_default).setOnClickListener(this);//全部商品下按钮组 -> 默认

        //滑动监听
        rv_shop_details.addOnScrollListener(new TopRecyclerListener());

        rg_shop_details.check(R.id.rb_shop_details_sy);
        rg_item_head_shop_details.check(R.id.rb_item_head_shop_details_sy);

        rg_shop_details_all_goods_type.check(R.id.rb_shop_details_all_goods_default);
        rg_item_head_shop_details_all_goods_type.check(R.id.rb_item_head_shop_details_all_goods_default);


        ptrl_shop_details.setRefreshListener(this);
        ptrl_shop_details.setCanRefresh(false);

    }

    //商品列表返回接口
    @Override
    public void goodsListSuccess(GoodsListEntity entity) {
        if ("1".equals(entity.statues) && entity.values != null){
            if (page == 1){
                adapter.setNewData(entity.values);
            }else {
                adapter.addData(entity.values);
            }
        }
    }

    //店铺商品返回接口
    @Override
    public void shopGoodsSuccess(ShopGoodsEntity entity) {

    }

    //品牌商品列表返回接口
    @Override
    public void brandGoodsSuccess(BrandGoodsEntity entity) {

    }

    @Override
    public void showDialog() {

    }

    @Override
    public void hideDialog() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_shop_details_back://返回上一页
                getActivity().finish();
                break;
            case R.id.ll_shop_details_search://搜索框
                Toast.makeText(getContext(), "搜索框", Toast.LENGTH_SHORT).show();
                break;
            case R.id.iv_shop_details_category://品类
                initMenu();
                break;
            case R.id.iv_shop_details_share://分享
                break;
            case R.id.tv_shop_details_spfl://商品分类
                initMenu();
                break;
            case R.id.tv_shop_details_dpjj://店铺简介
                break;
            case R.id.iv_item_head_shop_details_go_fav://店铺关注
                Toast.makeText(getContext(), "关注", Toast.LENGTH_SHORT).show();
                break;
            case R.id.rb_shop_details_all_goods_default:////隐藏的全部商品下的单选按钮组 -> 默认
                if (!first){
                    Toast.makeText(getContext(), "123", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.rb_item_head_shop_details_all_goods_default:////全部商品下按钮组 -> 默认
                if (!first){
                    Toast.makeText(getContext(), "321", Toast.LENGTH_SHORT).show();
                }
                break;

        }
    }

    private void initMenu() {
        SlidingMenu slidingMenu = new SlidingMenu(getContext());
        slidingMenu.setMode(SlidingMenu.RIGHT);
        slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
        slidingMenu.setBehindOffset(200);
        slidingMenu.setMenu(R.layout.slidemenu);
//        Button bt1= slidingMenu.getMenu().findViewById(R.id.bt1);
//        bt1.setOnClickListener(this);
        slidingMenu.attachToActivity(getActivity(),SlidingMenu.SLIDING_CONTENT);

    }


    @Override
    public void refresh() {

    }

    @Override
    public void loadMore() {
        page++;
        initData();
        ptrl_shop_details.finishLoadMore();
    }

    //RecyclerView滑动监听器
    private class TopRecyclerListener extends RecyclerView.OnScrollListener{
        @Override
        public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            totalY += dy;

//            if (totalY >= rl_item_head_shop_details_bg_Height / 4){
//                rg_shop_details.setVisibility(View.VISIBLE);
//            }else{
//                rg_shop_details.setVisibility(View.GONE);
//            }

            if (totalY >= 400){//滑动超过头布局背景

                if (rg_shop_details.getCheckedRadioButtonId() == R.id.rb_shop_details_all_goods || rg_item_head_shop_details.getCheckedRadioButtonId() == R.id.rb_item_head_shop_details_all_goods){
                    if (!cx) {
                        cx = true;
                        rg_shop_details_all_goods_type.setVisibility(View.VISIBLE);
                        rg_item_head_shop_details_all_goods_type.setOnCheckedChangeListener(null);
                        rg_shop_details_all_goods_type.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                            @Override
                            public void onCheckedChanged(RadioGroup group, int checkedId) {
                                int all_goods_id = 0;
                                switch (checkedId) {
                                    case R.id.rb_shop_details_all_goods_default://默认
                                        all_goods_id = R.id.rb_item_head_shop_details_all_goods_default;
                                        break;
                                    case R.id.rb_shop_details_all_goods_new://新品
                                        all_goods_id = R.id.rb_item_head_shop_details_all_goods_new;
                                        break;
                                    case R.id.rb_shop_details_all_goods_rq://人气
                                        all_goods_id = R.id.rb_item_head_shop_details_all_goods_rq;
                                        break;
                                    case R.id.rb_shop_details_all_goods_jg://价格
                                        all_goods_id = R.id.rb_item_head_shop_details_all_goods_jg;
                                        break;
                                    case R.id.rb_shop_details_all_goods_sx://筛选
                                        all_goods_id = R.id.rb_item_head_shop_details_all_goods_sx;
                                        break;
                                }

                                if (first && checkedId == R.id.rb_shop_details_all_goods_default){
                                    first = false;
                                }else{
                                    first = true;
                                }
                                page = 1;
                                p.goodsListRequest("1", "" + page);
                                rg_item_head_shop_details_all_goods_type.check(all_goods_id);
                                totalY = 0;
                            }
                        });
                    }
                }else{
                    rg_shop_details.setVisibility(View.VISIBLE);
                    rg_item_head_shop_details.setOnCheckedChangeListener(null);
                    //顶部隐藏按钮组
                    rg_shop_details.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(RadioGroup group, int checkedId) {
                            int tb_id = 0;
                            switch (checkedId) {
                                case R.id.rb_shop_details_sy:
                                    tb_id = R.id.rb_item_head_shop_details_sy;
                                    break;
                                case R.id.rb_shop_details_new:
                                    tb_id = R.id.rb_item_head_shop_details_new;
                                    break;
                                case R.id.rb_shop_details_all_goods:
                                    tb_id = R.id.rb_item_head_shop_details_all_goods;
                                    break;
                            }

                            rg_item_head_shop_details_all_goods_type.setVisibility(View.GONE);
                            if (checkedId == R.id.rb_shop_details_all_goods){
                                rg_item_head_shop_details_all_goods_type.setVisibility(View.VISIBLE);
                            }else{
                                cx = false;
                            }
                            rv_shop_details.scrollToPosition(0);
                            page = 1;
                            p.goodsListRequest("1", "" + page);
                            rg_item_head_shop_details.check(tb_id);
                            totalY = 0;
                            rg_shop_details.setVisibility(View.GONE);
                        }
                    });
                }

            }else{//滑动不超过头布局背景
                rg_shop_details.setVisibility(View.GONE);
                rg_shop_details_all_goods_type.setVisibility(View.GONE);
                rg_shop_details.setOnCheckedChangeListener(null);

                //头布局按钮组
                rg_item_head_shop_details.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(RadioGroup group, int checkedId) {
                        int tb_id = 0;
                        switch (checkedId) {
                            case R.id.rb_item_head_shop_details_sy:
                                tb_id = R.id.rb_shop_details_sy;
                                break;
                            case R.id.rb_item_head_shop_details_new:
                                tb_id = R.id.rb_shop_details_new;
                                break;
                            case R.id.rb_item_head_shop_details_all_goods:
                                tb_id = R.id.rb_shop_details_all_goods;
                                break;
                        }

                        rg_item_head_shop_details_all_goods_type.setVisibility(View.GONE);
                        if (checkedId == R.id.rb_item_head_shop_details_all_goods){
                            rg_item_head_shop_details_all_goods_type.setVisibility(View.VISIBLE);
                        }else{
                            cx = false;
                        }
                        rv_shop_details.scrollToPosition(0);
                        page = 1;
                        p.goodsListRequest("1", "" + page);
                        rg_shop_details.check(tb_id);
                        totalY = 0;
                        rg_shop_details.setVisibility(View.GONE);
                    }
                });


                rg_shop_details_all_goods_type.setOnCheckedChangeListener(null);
                rg_item_head_shop_details_all_goods_type.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(RadioGroup group, int checkedId) {
                        int all_goods_id = 0;
                        switch (checkedId){
                            case R.id.rb_item_head_shop_details_all_goods_default://默认
                                all_goods_id = R.id.rb_shop_details_all_goods_default;
                                break;
                            case R.id.rb_item_head_shop_details_all_goods_new://新品
                                all_goods_id = R.id.rb_shop_details_all_goods_new;
                                break;
                            case R.id.rb_item_head_shop_details_all_goods_rq://人气
                                all_goods_id = R.id.rb_shop_details_all_goods_rq;
                                break;
                            case R.id.rb_item_head_shop_details_all_goods_jg://价格
                                all_goods_id = R.id.rb_shop_details_all_goods_jg;
                                break;
                            case R.id.rb_item_head_shop_details_all_goods_sx://筛选
                                all_goods_id = R.id.rb_shop_details_all_goods_sx;
                                break;
                        }
                        if (first && checkedId == R.id.rb_item_head_shop_details_all_goods_default){
                            first = false;
                        }else{
                            first = true;
                        }

                        rv_shop_details.scrollToPosition(0);
                        page = 1;
                        p.goodsListRequest("1", "" + page);
                        rg_shop_details_all_goods_type.check(all_goods_id);
                        totalY = 0;
                    }
                });

            }



        }
    }
}
